/**
 * 登录用户信息
 */
const state = () => ({
  userInfo: "{}",     //用户信息
  userName: "",        //用户名
  nickName: "",        //用户昵称
  currBasin: null,    //当前探区
  currBasinId: null,    //当前探区ID
  userRoles: []        //当前登陆用户的权限
})

const mutations = {
  // set当前探区
  setCurrBasin(state, basin) {
    state.currBasin = basin;
    localStorage.setItem('basin', basin);
  },
  //登陆成功，保存用户信息
  loginSucess(state, data) {
    if (data) {
      state.userName = data.userName;
      localStorage.setItem('userName', state.userName);
      state.nickName = data.nickName;
      localStorage.setItem('nickName', state.nickName);
      state.currBasin = data.currBasin; //保存当前登录的探区
      localStorage.setItem('basin', data.currBasin);
      state.currBasinId = data.basinId; //保存当前登录的探区ID
      localStorage.setItem('basinId', data.basinId);
      state.userRoles = data.userRoles; //用户权限信息
      localStorage.setItem('roles', data.userRoles)
    } else {
      state.currBasin = null;
      localStorage.removeItem('userName');
      localStorage.removeItem('nickName');
      localStorage.removeItem('basin');
      localStorage.removeItem('basinId');
      localStorage.removeItem('roles');
      localStorage.removeItem('token');
    }

  }
}

export default {
  namespaced: true,
  state,
  mutations
}